package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.MoreZdAdapter;
import com.wbdgj.adapter.WbAdapter;
import com.wbdgj.adapter.ZdAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.MyGridView;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullLinearLayout;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

public class MoreZdActivity extends BaseActivity{

    private String key, COORDSX, COORDSY;
    private Context context = this;

    @BindView(R.id.demo_listview)
    MyGridView myPullListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    private int page = 1;
    private int totalPage;
    private ArrayList<LinkedTreeMap> linkedTreeMapList;
    private MoreZdAdapter zdAdapter;
    private DecimalFormat decimalFormat;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_more_zd;
    }

    @Override
    protected void initView() {
        decimalFormat = new DecimalFormat("###################.###########");
        listAll();
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        listAll();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page = page + 1;
                if (page > totalPage){
                    ToastUtils.toastShort("已无更多");
                }else{
                    listAll();
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });

        myPullListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!zdAdapter.hasNoData){
                    startActivity(new Intent(context, ZdDetailActivity.class).putExtra("id", zdAdapter.getItem(i).get("USER_ID")+""));
                }
            }
        });
    }



    private void listAll() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().listAll(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), "10000", page+"", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CITY));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    ArrayList<LinkedTreeMap> linkedTreeMapArrayList = (ArrayList<LinkedTreeMap>) linkedTreeMap.get("bossuserLogList");
                    LinkedTreeMap pageLinkedTreeMap = (com.google.gson.internal.LinkedTreeMap) linkedTreeMap.get("page");
                    totalPage = Integer.parseInt(decimalFormat.format(pageLinkedTreeMap.get("totalPage")));
                    linkedTreeMapList = new ArrayList<>();
                    if (page == 1){
                        zdAdapter = new MoreZdAdapter(context, linkedTreeMapArrayList);
                        myPullListView.setAdapter(zdAdapter);

                    }else{
                        zdAdapter.loadMoreListView(linkedTreeMapList);
                        zdAdapter.notifyDataSetChanged();
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
